package nongchang.kuaijiecaifu.com.ncapp_android.ui.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ezy.lite.util.ContextUtil;
import nongchang.kuaijiecaifu.com.ncapp_android.Global;
import nongchang.kuaijiecaifu.com.ncapp_android.R;
import nongchang.kuaijiecaifu.com.ncapp_android.common.fragment.BaseFragment;

import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.data.HomeCartManager;


import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.event.CartCountChange;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.good.AllCart;

import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.good.HomeItemCart;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.UserPayActivity;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.adapter.MyExpandableListViewAdapter;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.popup.SureOrderDialog;

import nongchang.kuaijiecaifu.com.ncapp_android.ui.widget.LoadingLayout;
import nongchang.kuaijiecaifu.com.ncapp_android.util.RxBus;
import rx.Observable;
import rx.Subscriber;

/**
 * Created by Administrator on 2016/3/11 0011.
 */
public class MainCartFragment extends BaseFragment {
    @BindView(R.id.expand_listview)
    ExpandableListView expandableListView;
    @BindView(R.id.cb_img)
    CheckBox cbImg;
    @BindView(R.id.txt_total_money)
    TextView txtMoney;
    @BindView(R.id.rl_layout)
    RelativeLayout relativeLayout;
    @BindView(R.id.btn_pay_money)
    TextView btnPay;
    @BindView(R.id.loading)
    LoadingLayout mLoading;
    private MyExpandableListViewAdapter adapter;

    DataSetObserver dataSetObserver = new DataSetObserver() {
        @Override
        public void onChanged() {
            super.onChanged();
            float sum = 0;
            int total=0;
            for (int i = 0, size = HomeCartManager.cartList.size(); i < size; i++) {
                if (HomeCartManager.cartList.get(i).isSelect) {
                    sum += HomeCartManager.cartList.get(i).money;
                    total+=1;
                }
            }
            txtMoney.setText("商品总计：￥" + sum + "   配送费：￥6");
            btnPay.setText("结算（"+total+"）");
        }
    };
    private int keyHeight = 0;
    private int screenHeight = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_main_basket_cart, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, getView());
        screenHeight = getActivity().getWindowManager().getDefaultDisplay().getHeight();
        //阀值设置为屏幕高度的1/3
        keyHeight = screenHeight / 3;
        load();
        RxBus.ofType(CartCountChange.class).subscribe(event->{
            adapter.update(HomeCartManager.cartList);
            if(HomeCartManager.cartList.size()==0){
                mLoading.showEmpty();
            }else{
                mLoading.showContent();
            }
        });
    }

    private void load() {

                adapter = new MyExpandableListViewAdapter(getContext(), HomeCartManager.cartList);
                expandableListView.setAdapter(adapter);
                expandableListView.setGroupIndicator(null);
                adapter.registerDataSetObserver(dataSetObserver);
                adapter.update(HomeCartManager.cartList);
                if(HomeCartManager.cartList.size()==0){
                    mLoading.showEmpty();
                }else{
                    mLoading.showContent();
                }
    }

    @OnClick({R.id.btn_pay_money, R.id.cb_img})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_pay_money:
                ContextUtil.startActivity(getContext(), UserPayActivity.class);
                break;
            case R.id.cb_img:
                if (cbImg.isChecked()) {
                    for (int i = 0, size = HomeCartManager.cartList.size(); i < size; i++) {
                        HomeCartManager.cartList.get(i).isSelect = true;
                    }
                } else {
                    for (int i = 0, size = HomeCartManager.cartList.size(); i < size; i++) {
                        HomeCartManager.cartList.get(i).isSelect = false;
                    }
                }
                adapter.update(HomeCartManager.cartList);
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        relativeLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                if (oldBottom != 0 && bottom != 0 && (oldBottom - bottom > keyHeight)) {

//                    Toast.makeText(CartActivity.this, "监听到软键盘弹起...", Toast.LENGTH_SHORT).show();

                } else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > keyHeight)) {
//                    Toast.makeText(CartActivity.this, "监听到软件盘关闭...", Toast.LENGTH_SHORT).show();
                    expandableListView.post(new Runnable() {
                        @Override
                        public void run() {
                            expandableListView.requestFocus();
                        }
                    });
                }
            }
        });
    }
}
